<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="studentName">
        <el-input v-model="queryParams.studentName" placeholder="请输入姓名" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="学号" prop="studentNumber">
        <el-input v-model="queryParams.studentNumber" placeholder="请输入学号" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="专业" prop="studentMajor">
        <el-select v-model="queryParams.studentMajor" style="width: 120px" placeholder="请选择专业" clearable>
          <el-option v-for="dict in sys_major" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="班级" prop="studentClass">
        <el-select v-model="queryParams.studentClass" placeholder="请选择班级" clearable style="width: 200px">
          <el-option v-for="dict in sys_class" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <br>
      <!-- <el-form-item label="困难毕业生" prop="difficultFlag" style="width: 330px;" label-width="125px">
        <el-select v-model="queryParams.difficultFlag" style="width: 200px" placeholder="请选择是否是困难毕业生" clearable>
          <el-option v-for="dict in sys_yes_no" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="省级优秀毕业生" prop="provinceHonorFlag" style="width: 400px" label-width="125px">
        <el-select v-model="queryParams.provinceHonorFlag" style="width: 280px" placeholder="请选择是否是省级优秀毕业生" clearable>
          <el-option v-for="dict in sys_yes_no" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="中共党员" prop="cpcFlag" style="width: 300px">
        <el-select v-model="queryParams.cpcFlag" style="width: 200px" placeholder="请选择是否是中共党员" clearable>
          <el-option v-for="dict in sys_yes_no" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5" v-if="total <= 0">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['student:student:add']">新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['student:student:edit']">修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['student:student:remove']">删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['student:student:export']">导出
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="studentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--      <el-table-column label="主键" align="center" prop="id" />-->
      <!--      <el-table-column label="用户ID" align="center" prop="userId" />-->
      <el-table-column label="姓名" align="center" prop="studentName" />
      <el-table-column label="学号" align="center" prop="studentNumber" />
      <el-table-column label="专业" align="center" prop="studentMajor">
        <template #default="scope">
          <dict-tag :options="sys_major" :value="scope.row.studentMajor" />
        </template>
      </el-table-column>
      <el-table-column label="班级" align="center" prop="studentClass">
        <template #default="scope">
          <dict-tag :options="sys_class" :value="scope.row.studentClass" />
        </template>
      </el-table-column>
      <el-table-column label="本专科" align="center" prop="studentLevel">
        <template #default="scope">
          <dict-tag :options="sys_undergraduate_specialist" :value="scope.row.studentLevel" />
        </template>
      </el-table-column>
      <el-table-column label="学业导师" align="center" prop="academicAdvisorName" />
      <el-table-column label="导师部门" align="center" prop="advisorDepartmentName" />
      <el-table-column label="辅导员" align="center" prop="counselorName" />
      <el-table-column label="个人照片" align="center" prop="personalPhoto" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.personalPhoto" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="家长联系方式" align="center" prop="parent_phone" />
      <el-table-column label="备注" align="center" prop="remark" />
      <!-- <el-table-column label="角色" align="center" prop="roleId" /> -->
      <!-- <el-table-column label="困难毕业生" align="center" prop="difficultFlag">
        <template #default="scope">
          <dict-tag :options="sys_yes_no" :value="scope.row.difficultFlag" />
        </template>
      </el-table-column>
      <el-table-column label="省级优秀毕业生" align="center" prop="provinceHonorFlag">
        <template #default="scope">
          <dict-tag :options="sys_yes_no" :value="scope.row.provinceHonorFlag" />
        </template>
      </el-table-column>
      <el-table-column label="中共党员" align="center" prop="cpcFlag" width="50px">
        <template #default="scope">
          <dict-tag :options="sys_yes_no" :value="scope.row.cpcFlag" />
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['student:student:edit']">修改
          </el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['student:student:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改学生对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="studentRef" :model="form" :rules="rules" label-width="110px">
        <!--        <el-form-item label="用户ID" prop="userId">-->
        <!--          <el-input v-model="form.userId" placeholder="请输入用户ID" />-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="角色ID" prop="roleId">-->
        <!--          <el-input v-model="form.roleId" placeholder="请输入角色ID" />-->
        <!--        </el-form-item>-->
        <el-form-item label="姓名" prop="studentName">
          <el-input v-model="form.studentName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="学号" prop="studentNumber">
          <el-input v-model="form.studentNumber" placeholder="请输入学号" />
        </el-form-item>
        <el-form-item label="专业" prop="studentMajor">
          <el-select v-model="form.studentMajor" placeholder="请选择专业">
            <el-option v-for="dict in sys_major" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="班级" prop="studentClass">
          <el-select v-model="form.studentClass" placeholder="请选择班级">
            <el-option v-for="dict in sys_class" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="本专科" prop="studentLevel">
          <el-select v-model="form.studentLevel" placeholder="请选择本专科">
            <el-option v-for="dict in sys_undergraduate_specialist" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="导师部门" prop="advisorDepartmentId">
          <el-tree-select v-model="form.advisorDepartmentId" :data="deptOptions" filterable
            :props="{ value: 'id', label: 'label', children: 'children' }" value-key="id" placeholder="请选择导师部门"
            check-strictly @change="handleAdvisorDepartmentChange" />
          <!-- <el-input v-model="form.advisorDepartmentId" placeholder="请输入导师部门" /> -->
        </el-form-item>
        <el-form-item label="学业导师" prop="academicAdvisorId">
          <el-select v-model="form.academicAdvisorId" placeholder="请选择学业导师" clearable @focus="handleFocus">
            <el-option v-for="academicAdvisor in academicAdvisorList" :key="academicAdvisor.userId"
              :label="academicAdvisor.nickName" :value="academicAdvisor.userId" />
          </el-select>
          <!-- <el-input v-model="form.academicAdvisorId" placeholder="请输入学业导师" /> -->
        </el-form-item>
        <el-form-item label="辅导员" prop="counselorId">
          <el-select v-model="form.counselorId" placeholder="请选择辅导员" clearable>
            <el-option v-for="counselor in counselorist" :key="counselor.userId" :label="counselor.nickName"
              :value="counselor.userId" />
          </el-select>
          <!-- <el-input v-model="form.counselorId" placeholder="请输入辅导员" /> -->
        </el-form-item>
        <el-form-item label="个人照片" prop="personalPhoto">
          <image-upload v-model="form.personalPhoto" />
        </el-form-item>
        <el-form-item label="家长联系方式" prop="parent_phone">
          <el-input v-model="form.parent_phone" placeholder="请输入家长联系方式" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <!-- <el-form-item label="困难毕业生" prop="difficultFlag">
          <el-select v-model="form.difficultFlag" placeholder="请选择是否是困难毕业生" clearable>
            <el-option v-for="dict in sys_yes_no" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="省级优秀毕业生" prop="provinceHonorFlag">
          <el-select v-model="form.provinceHonorFlag" placeholder="请选择是否是省级优秀毕业生" clearable>
            <el-option v-for="dict in sys_yes_no" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="中共党员" prop="cpcFlag">
          <el-select v-model="form.cpcFlag" placeholder="请选择是否是中共党员" clearable>
            <el-option v-for="dict in sys_yes_no" :key="dict.value" :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item> -->
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Student">
import { listStudent, getStudent, delStudent, addStudent, updateStudent, getAdvisorList } from "@/api/student/student";
import { deptTreeSelect, listUser } from "@/api/system/user";
import { ElMessage } from 'element-plus'

const { proxy } = getCurrentInstance();
const {
  sys_major,
  sys_class,
  sys_undergraduate_specialist,
  sys_yes_no
} = proxy.useDict('sys_major', 'sys_class', 'sys_undergraduate_specialist', 'sys_yes_no');

const studentList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const deptOptions = ref(undefined);
const academicAdvisorList = ref(undefined);
const counselorist = ref(undefined);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    studentName: null,
    studentNumber: null,
    studentMajor: null,
    studentClass: null,
    difficultFlag: null,
    provinceHonorFlag: null,
    cpcFlag: null
  },
  rules: {
    studentName: [
      { required: true, message: "姓名不能为空", trigger: "blur" }
    ],
    studentNumber: [
      { required: true, message: "学号不能为空", trigger: "blur" }
    ],
    studentMajor: [
      { required: true, message: "专业不能为空", trigger: "change" }
    ],
    studentClass: [
      { required: true, message: "班级不能为空", trigger: "change" }
    ],
    studentLevel: [
      { required: true, message: "本专科不能为空", trigger: "change" }
    ],
    academicAdvisorId: [
      { required: true, message: "学业导师不能为空", trigger: "blur" }
    ],
    advisorDepartmentId: [
      { required: true, message: "导师部门不能为空", trigger: "blur" }
    ],
    counselorId: [
      { required: true, message: "辅导员不能为空", trigger: "blur" }
    ],
    personalPhoto: [
      { required: true, message: "个人照片不能为空", trigger: "blur" }
    ],
    parent_phone: [
      { required: true, message: "家长联系方式不能为空", trigger: "blur" }
    ],
    // difficultFlag: [
    //   {required: true, message: "是否为贫困生不能为空", trigger: "blur"}
    // ],
    // provinceHonorFlag: [
    //   {required: true, message: "家长联系方式不能为空", trigger: "blur"}
    // ],
    // cpcFlag: [
    //   {required: true, message: "家长联系方式不能为空", trigger: "blur"}
    // ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询学生列表 */
function getList() {
  loading.value = true;
  listStudent(queryParams.value).then(response => {
    console.log(queryParams.value)
    console.log(queryParams.difficultFlag)
    studentList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    userId: null,
    roleId: null,
    studentName: null,
    studentNumber: null,
    studentMajor: null,
    studentClass: null,
    studentLevel: null,
    academicAdvisorId: null,
    advisorDepartmentId: null,
    counselorId: null,
    personalPhoto: null,
    remark: null,
    parent_phone: null,
    difficultFlag: null,
    provinceHonorFlag: null,
    cpcFlag: null
  };
  proxy.resetForm("studentRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加学生";
  getDeptTree();
  loadCounselorist();
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getStudent(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改学生";
    getDeptTree();
    loadAdvisorList(form.value.advisorDepartmentId);
    loadCounselorist();
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["studentRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateStudent(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addStudent(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除学生编号为"' + _ids + '"的数据项？').then(function () {
    return delStudent(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('student/student/export', {
    ...queryParams.value
  }, `student_${new Date().getTime()}.xlsx`)
}
/** 查询部门下拉树结构 */
function getDeptTree() {
  deptTreeSelect().then(response => {
    deptOptions.value = response.data;
  });
}
//获取学业导师列表
function loadAdvisorList(deptId) {
  getAdvisorList(deptId).then(response => {
    academicAdvisorList.value = response;
  });
}

//获取辅导员列表counselorist
function loadCounselorist() {
  listUser().then(response => {
    counselorist.value = response.rows;
  });
}
const handleAdvisorDepartmentChange = (val) => {
  form.value.advisorDepartmentId = val;
  form.value.academicAdvisorId = null;
}
/** 查询用户列表 */
const handleFocus = () => {
  if (form.value.advisorDepartmentId) {
    loadAdvisorList(form.value.advisorDepartmentId)
  } else {
    ElMessage('请先选择部门');
  }
}
getList();
</script>
